package com.ruoyi.web.controller.business;

import com.ruoyi.business.domain.BusContract;
import com.ruoyi.business.domain.BusContractDetail;
import com.ruoyi.business.service.IBusContractService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 合同Controller
 * 
 * @author ruoyi
 * @date 2021-09-02
 */
@RestController
@RequestMapping("/business/contract")
public class BusContractController extends BaseController
{
    @Autowired
    private IBusContractService busContractService;

    /**
     * 查询合同列表
     */
    //@PreAuthorize("@ss.hasPermi('business:contract:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusContract busContract)
    {
        startPage();
        List<BusContractDetail> list = busContractService.getContractListDetail(busContract);
        return getDataTable(list);
    }

    /**
     * 导出合同列表
     */
    //@PreAuthorize("@ss.hasPermi('business:contract:export')")
    @Log(title = "合同", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BusContract busContract)
    {
//        List<BusContract> list = busContractService.selectBusContractList(busContract);
//        ExcelUtil<BusContract> util = new ExcelUtil<BusContract>(BusContract.class);
//       return util.exportExcel(list, "合同数据");
//        String fileName = busContractService.exportContractInfo();
        //return AjaxResult.success(fileName);

        return busContractService.exportContractInfo(busContract.getCustomerId());
    }

    /**
     * 获取合同详细信息
     */
   // @PreAuthorize("@ss.hasPermi('business:contract:query')")
    @GetMapping(value = "/{contractId}")
    public AjaxResult getInfo(@PathVariable("contractId") Long contractId)
    {
        return AjaxResult.success(busContractService.selectBusContractByContractId(contractId));
    }

    /**
     * 获取合同明细信息
     */
    //@PreAuthorize("@ss.hasPermi('business:contract:detail')")
    @GetMapping(value = "/detail/{contractId}")
    public AjaxResult getDetail(@PathVariable("contractId") Long contractId)
    {
        return AjaxResult.success(busContractService.getContractDetail(contractId));
    }

    /**
     * 新增合同
     */
   // @PreAuthorize("@ss.hasPermi('business:contract:add')")
    @Log(title = "合同", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusContract busContract)
    {
        if (busContractService.checkContractExists(busContract))
        {
            return AjaxResult.error("新增合同失败,合同编号'" + busContract.getContractNo() + "'已存在");
        }

        busContract.setCreateBy(getUsername());
        return toAjax(busContractService.insertBusContract(busContract));
    }

    /**
     * 修改合同
     */
   // @PreAuthorize("@ss.hasPermi('business:contract:edit')")
    @Log(title = "合同", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusContract busContract)
    {
        if (busContractService.checkContractExists(busContract))
        {
            return AjaxResult.error("修改合同失败,合同编号'" + busContract.getContractNo() + "'已存在");
        }
        busContract.setUpdateBy(getUsername());
        return toAjax(busContractService.updateBusContract(busContract));
    }

    /**
     * 删除合同
     */
   // @PreAuthorize("@ss.hasPermi('business:contract:remove')")
    @Log(title = "合同", businessType = BusinessType.DELETE)
	@DeleteMapping("/{contractIds}")
    public AjaxResult remove(@PathVariable Long[] contractIds)
    {
        return toAjax(busContractService.deleteBusContractByContractIds(contractIds));
    }

    /**
     * 查询合同列表
     */
    //@PreAuthorize("@ss.hasPermi('business:contract:remind')")
    @GetMapping("/remind")
    public AjaxResult remind()
    {
        List<BusContract> list = busContractService.getRemindContractList();
        return AjaxResult.success(list);
    }
}
